﻿using HKB.Common;
using HKB.Data;
using HKB.Web;
using HKB.Web.Admin;
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class Admin_Admin_UpdateAdZone : Admin, IRequiresSessionState
{
    private int AdID = -1;
    private int Howave_UserID = -1;
    private int ADUserID = -1;
    private string startstr = "//-----------------------开始";
    private string endstr = "//-----------------------结束";
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.admin.CheckAdminPower(3))
        {
            base.WriteLog("Modidy", Language.GetAdminMsg("Log/Access", "0"), "Ads");
            base.WriteRightErrMsg();
        }
        this.TxtEndTime.Attributes.Add("onClick", string.Concat(new string[]
		{
			"show_cele_date(",
			this.TxtEndTime.ClientID,
			",'','',",
			this.TxtEndTime.ClientID,
			");"
		}));
        if (!base.IsPostBack)
        {
            this.TxtEndTime.Text = DateTime.Now.AddDays(30.0).ToShortDateString();
            this.Panel2.Visible = (this.Panel3.Visible = (this.Panel4.Visible = (this.Panel5.Visible = false)));
            this.bandzonid();
            if (base.Request.QueryString["UpdateAdId"] != null)
            {
                this.AdID = int.Parse(base.Request.QueryString["UpdateAdID"].ToString());
                this.ButAdd.Visible = false;
                this.ButUpdate.Visible = true;
                this.BandSingleAd();
            }
            else
            {
                this.ButUpdate.Visible = false;
            }
        }
        if (base.Request.QueryString["UpdateAdId"] != null)
        {
            this.AdID = int.Parse(base.Request.QueryString["UpdateAdID"].ToString());
        }
        if (base.Request.QueryString["Add"] != null)
        {
            this.ButAdd.Visible = true;
            this.ButUpdate.Visible = false;
            this.Panel1.Visible = true;
        }
        if (this.ADType.SelectedValue != "1")
        {
            this.CountClick.Enabled = (this.Clicks.Enabled = false);
        }
    }
    private void bandzonid()
    {
        AdZoneBll adZoneBll = new AdZoneBll();
        this.ZoneID.DataSource = adZoneBll.GetAllZone();
        this.ZoneID.DataValueField = "zoneid";
        this.ZoneID.DataTextField = "zonename";
        this.ZoneID.DataBind();
    }
    protected void BandSingleAd()
    {
        AdBll adBll = new AdBll();
        DataRow dataRow = adBll.GetSingeleAd(this.AdID).Rows[0];
        string[] array = dataRow["ZoneID"].ToString().Split(new char[]
		{
			','
		});
        for (int i = 0; i < array.Length; i++)
        {
            foreach (ListItem listItem in this.ZoneID.Items)
            {
                if (listItem.Value.Trim() == array[i].Trim())
                {
                    listItem.Selected = true;
                }
            }
        }
        this.ADUserID = int.Parse(dataRow["userid"].ToString());
        this.AdID = int.Parse(dataRow["ADID"].ToString());
        this.ADType.SelectedValue = dataRow["ADType"].ToString();
        this.ADName.Value = dataRow["adname"].ToString();
        this.FlashUrl.Value = (this.ImgUrl.Value = dataRow["imgurl"].ToString());
        this.ImgWidth.Value = dataRow["imgwidth"].ToString();
        this.ImgHeight.Value = dataRow["imgheight"].ToString();
        this.LinkUrl.Value = dataRow["linkurl"].ToString();
        this.LinkAlt.Value = dataRow["linkalt"].ToString();
        this.FlashWidth.Value = dataRow["imgwidth"].ToString();
        this.FlashHeight.Value = dataRow["imgheight"].ToString();
        this.FlasWmode.SelectedValue = dataRow["FlashWmode"].ToString();
        this.Views.Value = dataRow["views"].ToString();
        this.Priority.Value = dataRow["Priority"].ToString();
        this.LinkUrl.Value = dataRow["linkurl"].ToString();
        this.ADText.Value = (this.ADCode.Value = (this.WebFileUrl.Value = (this.ADIntro.Value = dataRow["ADIntro"].ToString())));
        this.LinkTarget.SelectedValue = dataRow["LinkTarget"].ToString();
        this.LinkAlt.Value = dataRow["LinkAlt"].ToString();
        this.Clicks.Text = dataRow["clicks"].ToString();
        bool flag = bool.Parse(dataRow["passed"].ToString());
        bool flag2 = bool.Parse(dataRow["CountView"].ToString());
        bool flag3 = bool.Parse(dataRow["countclick"].ToString());
        this.TxtEndTime.Text = DateTime.Parse(dataRow["EndTime"].ToString()).ToShortDateString();
        if (dataRow["ADType"].ToString() == "1")
        {
            this.UploadImgUrl.ImageUrl = dataRow["imgurl"].ToString();
            this.UploadImgUrl.Width = 150;
            this.UploadImgUrl.Height = 150;
            this.UploadImgUrl.Visible = true;
        }
        if (flag)
        {
            this.Passed.Checked = true;
        }
        if (flag3)
        {
            this.CountClick.Checked = true;
        }
        if (flag2)
        {
            this.CountView.Checked = true;
        }
        string selectedValue = this.ADType.SelectedValue;
        if (selectedValue != null)
        {
            if (!(selectedValue == "1"))
            {
                if (!(selectedValue == "2"))
                {
                    if (!(selectedValue == "3"))
                    {
                        if (!(selectedValue == "4"))
                        {
                            if (selectedValue == "5")
                            {
                                this.Panel5.Visible = true;
                                this.Panel2.Visible = (this.Panel3.Visible = (this.Panel4.Visible = (this.Panel1.Visible = false)));
                            }
                        }
                        else
                        {
                            this.Panel4.Visible = true;
                            this.Panel2.Visible = (this.Panel3.Visible = (this.Panel1.Visible = (this.Panel5.Visible = false)));
                        }
                    }
                    else
                    {
                        this.Panel3.Visible = true;
                        this.Panel2.Visible = (this.Panel1.Visible = (this.Panel4.Visible = (this.Panel5.Visible = false)));
                    }
                }
                else
                {
                    this.Panel2.Visible = true;
                    this.Panel1.Visible = (this.Panel3.Visible = (this.Panel4.Visible = (this.Panel5.Visible = false)));
                }
            }
            else
            {
                this.Panel1.Visible = true;
                this.Panel2.Visible = (this.Panel3.Visible = (this.Panel4.Visible = (this.Panel5.Visible = false)));
            }
        }
        this.H_ADID.Value = this.AdID.ToString();
        this.H_ZoneIDArray.Value = dataRow["ZoneID"].ToString();
        this.H_ADName.Value = dataRow["adname"].ToString();
    }
    protected void RadLType_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedValue = this.ADType.SelectedValue;
        if (selectedValue != null)
        {
            if (!(selectedValue == "1"))
            {
                if (!(selectedValue == "2"))
                {
                    if (!(selectedValue == "3"))
                    {
                        if (!(selectedValue == "4"))
                        {
                            if (selectedValue == "5")
                            {
                                this.Panel5.Visible = true;
                                this.Panel1.Visible = (this.Panel3.Visible = (this.Panel4.Visible = (this.Panel2.Visible = false)));
                            }
                        }
                        else
                        {
                            this.Panel4.Visible = true;
                            this.Panel1.Visible = (this.Panel3.Visible = (this.Panel2.Visible = (this.Panel5.Visible = false)));
                        }
                    }
                    else
                    {
                        this.Panel3.Visible = true;
                        this.Panel1.Visible = (this.Panel2.Visible = (this.Panel4.Visible = (this.Panel5.Visible = false)));
                    }
                }
                else
                {
                    this.Panel2.Visible = true;
                    this.Panel1.Visible = (this.Panel3.Visible = (this.Panel4.Visible = (this.Panel5.Visible = false)));
                }
            }
            else
            {
                this.Panel1.Visible = true;
                this.Panel2.Visible = (this.Panel3.Visible = (this.Panel4.Visible = (this.Panel5.Visible = false)));
            }
        }
        if (this.ADType.SelectedValue != "1")
        {
            this.CountClick.Enabled = (this.Clicks.Enabled = false);
        }
    }
    protected void UpdateZoneJS(int ADID, int ADType, string ADName, string ImgUrl, int ImgWidth, int ImgHeight, int FlashWmode, string ADIntro, string LinkUrl, int LinkTarget, string LinkAlt, int Priority, int CountView, int CountClick, string EndTime, int ZoneID)
    {
        string text = ZoneID.ToString();
        AdZoneBll adZoneBll = new AdZoneBll();
        string text2 = adZoneBll.GetSingeAdZone(ZoneID).Rows[0]["ZoneJsName"].ToString();
        text2 = base.Server.MapPath("..") + "\\AD\\" + text2;
        if (!File.Exists(text2))
        {
            this.AddAd();
        }
        else
        {
            StringBuilder stringBuilder = new StringBuilder();
            StringBuilder stringBuilder2 = new StringBuilder();
            using (StreamReader streamReader = new StreamReader(text2, Encoding.Default))
            {
                string str;
                while ((str = streamReader.ReadLine()) != null)
                {
                    stringBuilder2.Append(str + "\r\n");
                }
                streamReader.Close();
            }
            try
            {
                stringBuilder2.Replace("ZoneAD_" + text + ".Show();", "");
            }
            catch (Exception ex)
            {
                base.Response.Write(ex.ToString());
            }
            try
            {
                string text3 = "";
                if (HttpContext.Current.Request.Url.Port.ToString() != "80")
                {
                    text3 = ":" + HttpContext.Current.Request.Url.Port.ToString();
                }
                string str2 = this.startstr + ADName.Trim() + "\n";
                string str3 = this.endstr + ADName.Trim() + "\n";
                string str4 = string.Concat(new string[]
				{
					" var objAD = new ObjectAD();\r\nobjAD.ADID           =",
					ADID.ToString().Trim(),
					";\r\nobjAD.ADType         =",
					ADType.ToString().Trim(),
					";\r\nobjAD.ADName         = \"",
					ADName.Trim(),
					"\";\r\nobjAD.ImgUrl         = \"",
					ImgUrl.Trim(),
					"\";\r\nobjAD.ImgWidth       = ",
					ImgWidth.ToString().Trim(),
					";\r\nobjAD.ImgHeight      = ",
					ImgHeight.ToString().Trim(),
					";\r\nobjAD.FlashWmode     = ",
					FlashWmode.ToString().Trim(),
					";\r\nobjAD.ADIntro        = \"",
					ADIntro.Trim(),
					"\";\r\nobjAD.LinkUrl        = \"",
					LinkUrl.ToString().Trim(),
					"\";\r\nobjAD.LinkTarget     = ",
					LinkTarget.ToString().Trim(),
					";\r\nobjAD.LinkAlt        = \"",
					LinkAlt.ToString().Trim(),
					"\";\r\nobjAD.Priority       = ",
					Priority.ToString().ToString(),
					";\r\nobjAD.CountView      = ",
					CountView.ToString().Trim(),
					";\r\nobjAD.CountClick     = ",
					CountClick.ToString().Trim(),
					";\r\nobjAD.EndTime =\"",
					EndTime.ToString().Trim(),
					"\";\r\nobjAD.InstallDir     =\"http://",
					HttpContext.Current.Request.Url.Host.ToString(),
					text3,
					"/\";\r\nobjAD.ADDIR          =\"AD\"; \r\nZoneAD_",
					text.ToString().Trim(),
					".AddAD(objAD);\r\n"
				});
                stringBuilder2.Insert(stringBuilder2.Length, str2 + str4 + str3);
            }
            catch (Exception ex)
            {
                base.Response.Write(ex.ToString());
            }
            try
            {
                stringBuilder2.Insert(stringBuilder2.Length, "ZoneAD_" + text + ".Show();\r\n");
                using (StreamWriter streamWriter = new StreamWriter(text2, false, Encoding.GetEncoding("GB2312")))
                {
                    streamWriter.WriteLine(stringBuilder2);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
            }
            catch (Exception ex)
            {
                base.Response.Write(ex.ToString());
            }
        }
    }
    private void DelZoneJSRow(string ZoneID, string ADName)
    {
        string text = ZoneID.ToString();
        AdZoneBll adZoneBll = new AdZoneBll();
        string text2 = "";
        DataTable singeAdZone = adZoneBll.GetSingeAdZone(int.Parse(ZoneID));
        if (singeAdZone != null && singeAdZone.Rows.Count >= 1)
        {
            text2 = singeAdZone.Rows[0]["ZoneJsName"].ToString();
            text2 = base.Server.MapPath("..") + "\\AD\\" + text2;
            StringBuilder stringBuilder = new StringBuilder();
            StringBuilder stringBuilder2 = new StringBuilder();
            try
            {
                using (StreamReader streamReader = new StreamReader(text2, Encoding.Default))
                {
                    string str;
                    while ((str = streamReader.ReadLine()) != null)
                    {
                        stringBuilder2.Append(str + "\r\n");
                    }
                    streamReader.Close();
                }
            }
            catch (Exception ex)
            {
                base.Response.Write(ex.ToString());
            }
            try
            {
                string value = this.startstr + ADName.Trim();
                string text3 = this.endstr + ADName.Trim();
                string text4 = stringBuilder2.ToString();
                int num = text4.IndexOf(value);
                if (num > 0)
                {
                    string text5 = text4.Substring(num, text4.IndexOf(text3) - num + text3.Length);
                    if (text4.Contains(text5))
                    {
                        text4 = text4.Replace(text5, "");
                    }
                }
                stringBuilder.Append(text4);
            }
            catch (Exception ex)
            {
                base.Response.Write(ex.ToString());
                base.Response.End();
            }
            try
            {
                using (StreamWriter streamWriter = new StreamWriter(text2, false, Encoding.GetEncoding("GB2312")))
                {
                    streamWriter.WriteLine(stringBuilder);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
            }
            catch (Exception ex)
            {
                base.Response.Write(ex.ToString());
            }
        }
    }
    protected void ButAdd_Click(object sender, EventArgs e)
    {
        if (base.IsValid)
        {
            bool passed = false;
            bool countView = false;
            bool countClick = false;
            int countView2 = 0;
            int countClick2 = 0;
            string text = "";
            int imgWidth = 0;
            int num = 0;
            string imgUrl = "";
            string text2 = "";
            foreach (ListItem listItem in this.ZoneID.Items)
            {
                if (listItem.Selected)
                {
                    text2 = text2 + listItem.Value + ",";
                }
            }
            if (text2.Length > 2)
            {
                text2 = text2.Remove(text2.Length - 1);
            }
            if (this.Passed.Checked)
            {
                passed = true;
            }
            if (this.CountView.Checked)
            {
                countView2 = 1;
                countView = true;
            }
            if (this.CountClick.Checked)
            {
                countClick2 = 1;
                countClick = true;
            }
            string selectedValue = this.ADType.SelectedValue;
            if (selectedValue != null)
            {
                if (!(selectedValue == "1"))
                {
                    if (!(selectedValue == "2"))
                    {
                        if (!(selectedValue == "3"))
                        {
                            if (!(selectedValue == "4"))
                            {
                                if (selectedValue == "5")
                                {
                                    this.Panel5.Visible = true;
                                    this.Panel2.Visible = (this.Panel3.Visible = (this.Panel4.Visible = (this.Panel1.Visible = false)));
                                    text = this.WebFileUrl.Value;
                                    imgWidth = 0;
                                    num = 0;
                                    imgUrl = "";
                                }
                            }
                            else
                            {
                                this.Panel4.Visible = true;
                                this.Panel2.Visible = (this.Panel3.Visible = (this.Panel1.Visible = (this.Panel5.Visible = false)));
                                text = this.ADCode.Value;
                                imgWidth = 0;
                                num = 0;
                                imgUrl = "";
                            }
                        }
                        else
                        {
                            this.Panel3.Visible = true;
                            this.Panel2.Visible = (this.Panel1.Visible = (this.Panel4.Visible = (this.Panel5.Visible = false)));
                            text = this.ADText.Value;
                            imgWidth = 0;
                            num = 0;
                            imgUrl = "";
                        }
                    }
                    else
                    {
                        this.Panel2.Visible = true;
                        this.Panel1.Visible = (this.Panel3.Visible = (this.Panel4.Visible = (this.Panel5.Visible = false)));
                        text = "";
                        imgWidth = int.Parse(this.FlashWidth.Value);
                        num = int.Parse(this.FlashHeight.Value);
                        imgUrl = this.FlashUrl.Value;
                    }
                }
                else
                {
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = (this.Panel3.Visible = (this.Panel4.Visible = (this.Panel5.Visible = false)));
                    text = this.ADIntro.Value;
                    imgWidth = int.Parse(this.ImgWidth.Value);
                    num = int.Parse(this.ImgHeight.Value);
                    imgUrl = this.ImgUrl.Value;
                }
            }
            AdBll adBll = new AdBll();
            int aDID = adBll.InsertAD(this.Howave_UserID, text2, int.Parse(this.ADType.SelectedValue), this.ADName.Value, imgUrl, imgWidth, num, int.Parse(this.FlasWmode.SelectedValue), text, this.LinkUrl.Value, int.Parse(this.LinkTarget.SelectedValue), this.LinkAlt.Value, int.Parse(this.Priority.Value), "", int.Parse(this.Views.Value), countView, countClick, int.Parse(this.Clicks.Text), passed, DateTime.Parse(this.TxtEndTime.Text));
            this.ButAdd.Text = "增加成功";
            try
            {
                if (this.Passed.Checked)
                {
                    foreach (ListItem listItem in this.ZoneID.Items)
                    {
                        if (listItem.Selected)
                        {
                            int zoneID = int.Parse(listItem.Value);
                            this.UpdateZoneJS(aDID, int.Parse(this.ADType.SelectedValue), this.ADName.Value, imgUrl, imgWidth, num, int.Parse(this.FlasWmode.SelectedValue), text, this.LinkUrl.Value, int.Parse(this.LinkTarget.SelectedValue), this.LinkAlt.Value, int.Parse(this.Priority.Value), countView2, countClick2, this.TxtEndTime.Text, zoneID);
                        }
                    }
                }
                MessageBox.ShowAndRedirect(this, "添加成功", "Admin_ManageAd.aspx");
            }
            catch (Exception ex)
            {
                ex.WriteErrorLog(new string[0]);
                this.ButAdd.Text = "成功写入数据库，文件更新失败，可能没有写入权限！";
            }
        }
    }
    protected void ButUpdate_Click(object sender, EventArgs e)
    {
        this.AddAd();
    }
    protected void AddAd()
    {
        if (base.IsValid)
        {
            bool passed = false;
            bool countView = false;
            bool countClick = false;
            int countView2 = 0;
            int countClick2 = 0;
            string text = "";
            int imgWidth = 0;
            int num = 0;
            string imgUrl = "";
            string text2 = "";
            foreach (ListItem listItem in this.ZoneID.Items)
            {
                if (listItem.Selected)
                {
                    text2 = text2 + listItem.Value + ",";
                }
            }
            if (text2.Contains(","))
            {
                text2 = text2.Remove(text2.Length - 1);
            }
            if (this.Passed.Checked)
            {
                passed = true;
            }
            if (this.CountView.Checked)
            {
                countView2 = 1;
                countView = true;
            }
            if (this.CountClick.Checked)
            {
                countClick2 = 1;
                countClick = true;
            }
            string selectedValue = this.ADType.SelectedValue;
            if (selectedValue != null)
            {
                if (!(selectedValue == "1"))
                {
                    if (!(selectedValue == "2"))
                    {
                        if (!(selectedValue == "3"))
                        {
                            if (!(selectedValue == "4"))
                            {
                                if (selectedValue == "5")
                                {
                                    this.Panel5.Visible = true;
                                    this.Panel2.Visible = (this.Panel3.Visible = (this.Panel4.Visible = (this.Panel1.Visible = false)));
                                    text = this.WebFileUrl.Value;
                                    imgWidth = 0;
                                    num = 0;
                                    imgUrl = "";
                                }
                            }
                            else
                            {
                                this.Panel4.Visible = true;
                                this.Panel2.Visible = (this.Panel3.Visible = (this.Panel1.Visible = (this.Panel5.Visible = false)));
                                text = this.ADCode.Value;
                                imgWidth = 0;
                                num = 0;
                                imgUrl = "";
                            }
                        }
                        else
                        {
                            this.Panel3.Visible = true;
                            this.Panel2.Visible = (this.Panel1.Visible = (this.Panel4.Visible = (this.Panel5.Visible = false)));
                            text = this.ADText.Value;
                            imgWidth = 0;
                            num = 0;
                            imgUrl = "";
                        }
                    }
                    else
                    {
                        this.Panel2.Visible = true;
                        this.Panel1.Visible = (this.Panel3.Visible = (this.Panel4.Visible = (this.Panel5.Visible = false)));
                        text = "";
                        imgWidth = int.Parse(this.FlashWidth.Value);
                        num = int.Parse(this.FlashHeight.Value);
                        imgUrl = this.FlashUrl.Value;
                    }
                }
                else
                {
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = (this.Panel3.Visible = (this.Panel4.Visible = (this.Panel5.Visible = false)));
                    text = this.ADIntro.Value;
                    imgWidth = int.Parse(this.ImgWidth.Value);
                    num = int.Parse(this.ImgHeight.Value);
                    imgUrl = this.ImgUrl.Value;
                }
            }
            AdBll adBll = new AdBll();
            adBll.UpdateAd(text2, int.Parse(this.ADType.SelectedValue), this.ADName.Value, imgUrl, imgWidth, num, int.Parse(this.FlasWmode.SelectedValue), text, this.LinkUrl.Value, int.Parse(this.LinkTarget.SelectedValue), this.LinkAlt.Value, int.Parse(this.Priority.Value), countView, "", int.Parse(this.Views.Value), int.Parse(this.Clicks.Text), countClick, passed, DateTime.Parse(this.TxtEndTime.Text), int.Parse(this.H_ADID.Value));
            string[] array = this.H_ZoneIDArray.Value.Split(new char[]
			{
				','
			});
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] != "")
                {
                    this.DelZoneJSRow(array[i], this.H_ADName.Value);
                }
            }
            if (this.Passed.Checked)
            {
                foreach (ListItem listItem in this.ZoneID.Items)
                {
                    if (listItem.Selected)
                    {
                        int zoneID = int.Parse(listItem.Value);
                        this.UpdateZoneJS(int.Parse(this.H_ADID.Value), int.Parse(this.ADType.SelectedValue), this.ADName.Value, imgUrl, imgWidth, num, int.Parse(this.FlasWmode.SelectedValue), text, this.LinkUrl.Value, int.Parse(this.LinkTarget.SelectedValue), this.LinkAlt.Value, int.Parse(this.Priority.Value), countView2, countClick2, this.TxtEndTime.Text, zoneID);
                    }
                }
            }
            this.BandSingleAd();
            base.Response.Write("<script language='javascript'>alert('修改成功')</script>");
        }
    }
}
